package com.friend.joey.applicationfriend.OldAcitivities;

import android.app.AlarmManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.friend.joey.applicationfriend.Classes.TimeDeal;
import com.friend.joey.applicationfriend.R;


/**
 * Created by Joey on 2017/4/26.
 */

public class FFnewActivity extends AppCompatActivity {
    private Button btn_back;
    private Button btn_finish;
    private EditText txt_title;
    private EditText txt_text;
    private TextView alerttime;
    private CheckBox checkBox;
    private Boolean isCheck;
    private Boolean isFirst;
    private PopupWindow popWnd;
    private View contentView;
    private TimeDeal timedeal;
    private TimeDeal partTime;
    private TimePupupWindow pupupWindow;
    private int hour;
    private int theme;
    private AlarmManager manager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        theme=getIntent().getIntExtra("theme",0);
        isCheck=false;
        switch(theme){
            case 0:
                setTheme(R.style.AppTheme);
                break;
            case 2:
                setTheme(R.style.GreenTheme);
                break;
        }
        setContentView(R.layout.ffnew);
        //Toast.makeText(this,Integer.toString(theme),Toast.LENGTH_LONG).show();
        btn_back=(Button)findViewById(R.id.button_fbback);
        btn_finish=(Button)findViewById(R.id.button_fbfinish);
        txt_text=(EditText)findViewById(R.id.txt_fbtext);
        txt_title=(EditText)findViewById(R.id.txt_fbtitle);
        checkBox=(CheckBox)findViewById(R.id.fbnewalert);
        alerttime=(TextView)findViewById(R.id.txt_bnewalerttime);

        btn_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isCheck){
                    if(timedeal.isAlertAbled()){
                        Toast.makeText(FFnewActivity.this,"true", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent();
                        intent.putExtra("add",true);
                        String title=txt_title.getText().toString();
                        if(title==""){
                            title="无标题";
                        }
                        intent.putExtra("title",title);
                        intent.putExtra("content",txt_text.getText().toString());
                        intent.putExtra("isChecked",true);
                        intent.putExtra("alertid",timedeal.GetTime());
                        intent.putExtra("year",timedeal.getYear());
                        intent.putExtra("month",timedeal.getMonth());
                        intent.putExtra("day",timedeal.getDay());
                        intent.putExtra("hour",timedeal.getHour());
                        intent.putExtra("min",timedeal.getMin());
                        FFnewActivity.this.setResult(RESULT_OK,intent);
                        FFnewActivity.this.finish();
                    }else{
                        Toast.makeText(FFnewActivity.this,"false", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Intent intent=new Intent();
                    intent.putExtra("add",true);
                    intent.putExtra("title",txt_title.getText().toString());
                    intent.putExtra("content",txt_text.getText().toString());
                    intent.putExtra("isChecked",false);
                    FFnewActivity.this.setResult(RESULT_OK,intent);
                    FFnewActivity.this.finish();
                }
            }
        });
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.putExtra("add",false);
                FFnewActivity.this.setResult(RESULT_OK,intent);
                FFnewActivity.this.finish();
            }
        });


        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    isFirst=true;
                    isCheck=true;
                    pupupWindow=new TimePupupWindow(LayoutInflater.from(FFnewActivity.this),getWindow().getDecorView(),TimePupupWindow.DATEPOP);
                    pupupWindow.setOnPopupWindowListener(new TimePupupWindow.ClickBack() {

                        @Override
                        public void onYesClick(TimeDeal time) {
                            timedeal=pupupWindow.getTime();
                            String time2=timedeal.GetAlertTimeString();
                            partTime=time;
                            //Toast.makeText(FFnewActivity.this,time2, Toast.LENGTH_SHORT).show();
                            alerttime.setText(time2);
                        }

                        @Override
                        public void onNoClick() {
                            checkBox.setChecked(false);
                            isCheck=false;
                        }
                    });

                }else{
                    isCheck=false;
                    alerttime.setText("");
                }
            }
        });

        alerttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isFirst=false;
                pupupWindow=new TimePupupWindow(LayoutInflater.from(FFnewActivity.this),getWindow().getDecorView(),timedeal);
                pupupWindow.setOnPopupWindowListener(new TimePupupWindow.ClickBack() {

                    @Override
                    public void onYesClick(TimeDeal time) {
                        timedeal=pupupWindow.getTime();
                        partTime=time;
                        String time2=timedeal.GetAlertTimeString();
                        alerttime.setText(time2);
                    }

                    @Override
                    public void onNoClick() {

                    }
                });
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent=new Intent();
        intent.putExtra("add",false);
        FFnewActivity.this.setResult(RESULT_OK,intent);
        FFnewActivity.this.finish();
    }
}
